<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="handleSearchList()" size="small">查询搜索</el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input
              v-model="listQuery.keyword"
              class="input-width"
              placeholder="帐户名"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加管理员</el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="adminTable" :data="list" style="width: 100%;" v-loading="listLoading" border>
        <el-table-column label="#" type="index" width="50" align="center"></el-table-column>
        <el-table-column label="头像" width="160" align="center">
          <template slot-scope="scope">
            <el-image
              style="width: 48px; height: 48px"
              :src="scope.row.avatarUrl"
              :fit="'fill'"
              :preview-src-list="[scope.row.avatarUrl || '']"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="账户名" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="手机号" align="center">
          <template slot-scope="scope">{{scope.row.phone}}</template>
        </el-table-column>
        <el-table-column label="管理员状态" width="160" align="center">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.status === '-1'">已注销</el-tag>
            <el-tag type="info" v-if="scope.row.status === '0'">停用</el-tag>
            <el-tag type="success" v-if="scope.row.status === '1'">正常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button type="success" size="mini" @click="handleEdit(scope.row)">修改</el-button>
            <el-button
              type="success"
              size="mini"
              v-if="scope.row.status === '0'"
              @click="changeMiniUserStatus(scope.row, '1')"
            >启用</el-button>
            <el-button
              type="info"
              size="mini"
              v-if="scope.row.status === '1'"
              @click="changeMiniUserStatus(scope.row, '0')"
            >停用</el-button>
            <el-button
              type="warning"
              size="mini"
              v-if="scope.row.status === '1'"
              @click="resetAdminUserPsw(scope.row)"
            >重置密码</el-button>
            <el-button
              type="danger"
              size="mini"
              v-if="scope.row.status !== '-1'"
              @click="changeMiniUserStatus(scope.row, '-1')"
            >注销</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 新增修改弹窗 -->

    <el-dialog
      :title="aeDialogType === 'add' ? '新增管理员' : ( aeDialogType === 'edit' ? '修改管理员' : '' )"
      :visible.sync="aeDialogVis"
      width="450px"
      :close-on-click-modal="false"
      @closed="handleCloseAeDialog"
    >
      <div class="dialog-main">
        <el-form
          :model="aeDialogForm"
          :rules="aeDialogFormRules"
          ref="aeDialogFormRef"
          label-width="80px"
          label-position="left"
        >
          <el-form-item label="账户名" prop="name">
            <el-input v-model="aeDialogForm.name" clearable></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="aeDialogForm.phone" maxlength="11" clearable></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" v-if="aeDialogType === 'edit'">
            <el-input type="password" v-model="aeDialogForm.password" clearable></el-input>
          </el-form-item>
          
          <el-form-item label="头像" prop="avatarUrl">
            <ImageUpload v-model="aeDialogForm.avatarUrl" @change="getAvatarUrl" />
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="handleCloseAeDialog">取 消</el-button>
        <el-button size="mini" type="primary" @click="handleAEConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAdminUserList,
  changeMiniUserStatus,
  getUpToken,
  uploadQNImage,
  addAdminUser,
  updateAdminUser,
  resetAdminUserPsw
} from "@/api/auser";
import ImageUpload from "@/components/image-upload";
import qiniu from "@/utils/qiniu";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  keyword: null
};
export default {
  name: "adminList",
  components: {
    ImageUpload
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      aeDialogVis: false,
      aeDialogType: "",
      aeDialogForm: {},
      aeDialogFormRules: {
        name: [{ required: true, message: "请输入账户名", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        avatarUrl: [{ required: true, message: "请选择头像", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    changeMiniUserStatus(row, status) {
      let tip = {
        "-1": { msg: "确定要注销该用户吗？", type: "danger" },
        "0": { msg: "确定要将该用户加入黑名单吗？", type: "info" },
        "1": { msg: "确定要启用该用户吗？", type: "success" }
      };

      this.$confirm(tip[status].msg, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: tip[status].type
      }).then(() => {
        changeMiniUserStatus({
          id: row.id,
          status: status
        }).then(response => {
          this.$message({
            type: "success",
            message: "操作成功"
          });
          this.getList();
        });
      });
    },
    getList() {
      this.listLoading = true;
      getAdminUserList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    handleAdd() {
      this.aeDialogVis = true;
      this.aeDialogType = "add";
    },
    handleEdit(row) {
      this.aeDialogVis = true;
      this.aeDialogType = "edit";
      this.aeDialogForm = JSON.parse(JSON.stringify(row));
    },
    handleAEConfirm() {
      this.$refs.aeDialogFormRef.validate(valid => {
        if (!valid) return;

        if (this.aeDialogType === "add") {
          addAdminUser({
            name: this.aeDialogForm.name,
            phone: this.aeDialogForm.phone,
            avatarUrl: this.aeDialogForm.avatarUrl
          }).then(res => {
            if (res.isSuccee) {
              this.$message.success(res.msg);
              this.getList();
              this.handleCloseAeDialog();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else if (this.aeDialogType === "edit") {
          updateAdminUser({
            id: this.aeDialogForm.id,
            name: this.aeDialogForm.name,
            password: this.aeDialogForm.password,
            phone: this.aeDialogForm.phone,
            avatarUrl: this.aeDialogForm.avatarUrl
          }).then(res => {
            if (res.isSuccee) {
              this.$message.success(res.msg);
              this.getList();
              this.handleCloseAeDialog();
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
    resetAdminUserPsw(row) {
      this.$confirm(`确定要重置${row.name}的密码吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      }).then(() => {
        resetAdminUserPsw({
          id: row.id,
        }).then(res => {
          if (res.isSuccee) {
              this.$message.success(res.msg);
              this.getList();
            } else {
              this.$message.error(res.msg);
            }
        });
      });
    },
    handleCloseAeDialog() {
      this.aeDialogVis = false;
      this.aeDialogType = "";
      this.aeDialogForm = {};
      this.$refs.aeDialogFormRef.clearValidate();
    },
    getAvatarUrl(file) {
      console.log("图片上传", file);

      if (file) {
        getUpToken()
          .then(res => {
            return uploadQNImage({
              token: res.uptoken,
              file: file.raw
            });
          })
          .then(res => {
            if (res.key) {
              this.$set(
                this.aeDialogForm,
                "avatarUrl",
                `${qiniu.domain}${res.key}`
              );
            }
          });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.dialog-main {
}
</style>
